<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
global $logid;
$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();

$infoMap = array(
	'detail_info_display' => '我的详细信息',
	'friend_display' => '我的好友',
	'collection_display' => '收藏店铺',
	'footprint_display' => '访问其他会员主页',
	'basic_info_display' => '我的基本信息',
	'add_shop' => '添加店铺',
	'add_product' => '添加商品',
	'add_friend_list' => '添加好友和好友列表',
	'delete_friend_list' => '删除好友和添加黑名单',
	'update_avatar' => '上传头像',
	'report_error' => '举报错误',
	'add_comment' => '发表评论',
	'private_info_display' => '账户私人信息',
	'msg_display' => '收发站内信消息',

);

//help function to seperate 0 and 1 to different array
function SeperateArray( $params ){
	$result = array();
	$result['private'] = array();
	$result['public'] = array();

	foreach( $params as $key=>$value ){
		if( $key != 'id' ){
			if( $value == 0){
				$result['private'][] = $key;
			}
			else{
				$result['public'][] = $key;
			}
		}
	}

	return $result;
}

//write to database
if(isset($_POST['action'])) {
	$action=trim($_POST['action']);
	if($action=='privacy_setting') {
		//use loop to ensure single entrance and single exit
		do{
			$params = array();
			//$params['id'] = intval($_POST['id']);
			$params['id'] = $logid;
			$params['basic_info_display'] = intval($_POST['basic_info_display']);
			$params['detail_info_display'] = intval($_POST['detail_info_display']);
			$params['friend_display'] = intval($_POST['friend_display']);
			$params['collection_display'] = intval($_POST['collection_display']);
			$params['footprint_display'] = intval($_POST['footprint_display']);
			$params['add_shop'] = intval($_POST['add_shop']);
			$params['add_product'] = intval($_POST['add_product']);
			$params['add_friend_list'] = intval($_POST['add_friend_list']);
			$params['delete_friend_list'] = intval($_POST['delete_friend_list']);
			$params['update_avatar'] = intval($_POST['update_avatar']);
			$params['report_error'] = intval($_POST['report_error']);
			$params['add_comment'] = intval($_POST['add_comment']);
			$params['private_info_display'] = intval($_POST['private_info_display']);
			$params['msg_display'] = intval($_POST['msg_display']);

			//first check params
			if( edp_is_empty_string($params['id'] ) )
				$errmsg->add( 'privacy_setting', '用户不合法！', 'error' );

			$pageVars = array_merge( $pageVars, $params );
			$pageVars['summary'] = SeperateArray( $params );
			// when there is error, break the loop
			if( $errmsg->size('privacy_setting') > 0 )
			{
				break;
			}

			if( !$userDao->UpdateUserPrivacySetting( $params ) ) {
				$errmsg->add( 'privacy_setting', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'privacy_setting', '更新成功', 'success' );
			}

			break;
		}while(1);
	}
	else{
		$errmsg->add( 'privacy_setting', '未知操作', 'error' );
	}
}
else{
	/*
	$userInfo = getUserFromCookie();
	if( $userInfo ){
		//get user basic_info from database
		 */
		$userMsgSetting = $userDao->GetUserPrivacySetting( $logid );
		if( $userMsgSetting ){
			$pageVars['id'] = $userMsgSetting['user_id'];
			$pageVars['basic_info_display'] = intval($userMsgSetting['basic_info_display']);
			$pageVars['detail_info_display'] = intval($userMsgSetting['detail_info_display']);
			$pageVars['friend_display'] = intval($userMsgSetting['friend_display']);
			$pageVars['collection_display'] = intval($userMsgSetting['collection_display']);
			$pageVars['footprint_display'] = intval($userMsgSetting['footprint_display']);
			$pageVars['add_shop'] = intval($userMsgSetting['add_shop']);
			$pageVars['add_product'] = intval($userMsgSetting['add_product']);
			$pageVars['add_friend_list'] = intval($userMsgSetting['add_friend_list']);
			$pageVars['delete_friend_list'] = intval($userMsgSetting['delete_friend_list']);
			$pageVars['update_avatar'] = intval($userMsgSetting['update_avatar']);
			$pageVars['report_error'] = intval($userMsgSetting['report_error']);
			$pageVars['add_comment'] = intval($userMsgSetting['add_comment']);
			$pageVars['private_info_display'] = intval($userMsgSetting['private_info_display']);
			$pageVars['msg_display'] = intval($userMsgSetting['msg_display']);

			$pageVars['summary'] = SeperateArray( $pageVars );
		}
		else{
			$errmsg->add( 'privacy_setting', '读取用户信息失败', 'error' );
		}
	/*
	}
	else{
		$errmsg->add( 'privacy_setting', '登录用户信息不合法', 'error' );
	}
	*/
}
?>
